package com.sgb.jicai.viewmodel;

import androidx.lifecycle.ViewModel;

import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.jicai.entity.FactoryParticipationEntity;
import com.sgb.jicai.entity.SubmitFactoryParticipationEntity;
import com.sgb.jicai.view.activity.FactoryParticipationActivity;
import com.sgb.link.entity.InvoiceManagementEntity;
import com.sgb.link.entity.WebSiteAddressEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import java.util.List;

public class FactoryParticipationModel extends ViewModel {
    FactoryParticipationActivity activity;
    public void setActivity(FactoryParticipationActivity factoryParticipationActivity) {
    this.activity = factoryParticipationActivity;
    }

    //供方发起——获取立即参与页面详情数据
    public void getProductDetailForAddOrder(String no) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getProductDetailForAddOrder(no).enqueue(new ICallBack<BaseEntity<List<FactoryParticipationEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<FactoryParticipationEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() == null || data.getState() == null) {
                    MToast.showToast(activity, "获取单据详情失败");
                    return;
                }
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    activity.updateView(data.getData());
                } else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

    //供方发起--立即参与，提交立即参与信息
    public void submitParticipationInfo(SubmitFactoryParticipationEntity entity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
       JiCaiBaseApi.getApi().submitParticipationInfo(entity).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    MToast.showToast(activity, "提交信息成功");
                    activity.submitSuccessDo();
                } else {
                    MToast.showToast(activity, "提交信息出错：" + data.getMsg());
                }
            }
        });
    }


    //遍历地址列表
    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
      JiCaiBaseApi.getApi().getNewUserAddress( "10", 1, 1).enqueue(new ICallBack<BaseEntity<ListResponse<List<WebSiteAddressEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<WebSiteAddressEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data!= null&&data.getData()!=null&&data.getData().getList()!=null){
                    activity.showAddressList(data.getData().getList());
                }
            }
        });
    }

    //遍历开票信息列表
    public void getBillList(String companyNo) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
       JiCaiBaseApi.getApi().billList(companyNo).enqueue(new ICallBack<BaseEntity<ListResponse<List<InvoiceManagementEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<InvoiceManagementEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() == null) return;
                if (data.getState().equals("ok")) {
                    activity.showBillList(data.getData().getList());
                } else {
                    assert data.getData() != null;
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }


}
